<?php

class User extends MX_Controller {

	function __construct()
	{
		parent::__construct();
	}

	function index()
	{
		redirect($this->referrer);
	}
	
	function change_password()
	{
		$content_data = array('unit_controller' => get_unit_controller($this),
			'current_role' => $this->common->current_role_key(),
			'roles' => $roles,
			'referrer' => $this->referrer);
			
		if ($this->common->current_user_id()>0) {
			$content = $this->load->view('form/change_password', $content_data, true);
		} else {
			$content = '';
		}

		$data = array(
			'application' => '',
			'application_title' => '',
			'page' => 'Change User Password',
			'navigation' => $this->load->view('nav/default', '', true),
			'content' => $content,		
		);
		
		$this->load->view('layout/default', $data);		
	}
	
	function process_change_password()
	{
		$old_pass = $this->input->post('old_pass');
		$new_pass_1 = $this->input->post('new_pass_1');
		$new_pass_2 = $this->input->post('new_pass_2');
		
		if (strcmp($new_pass_1, $new_pass_2)!=0) {
			// the new passes don't match
			$this->common->flash_message('New password confirmation failed. Your password is not changed.', 1);
		} else {
			if (strcmp($old_pass, $this->common->get_field_from_key('sys_m_user', 'password', $this->common->current_user_key()))!=0) {
				// the old pass is invalid
				$this->common->flash_message('Invalid current password. Your password is not changed.', 1);
			} else {
				if (!$this->db->query('UPDATE sys_m_user SET `password`='.$this->db->escape($new_pass_1).' WHERE nkey='.$this->db->escape($this->common->current_user_key()))) {
					$this->common->flash_message('Error updating data. Your password is not changed.', 1);
				} else {
					$this->common->flash_message('Your password is changed successfully.', -1);
				}
			}
		}
			
		redirect($this->referrer);	
	}
	
	function change_role()
	{
		$this->db->select('key_sys_role')->from('sys_x_user_role')->where(array('key_sys_user' => $this->common->current_user_key()));
		$result = $this->db->get()->result_array();
		
		$roles = array();
		foreach ($result as $idx => $val) {
			
			$roles[$val['key_sys_role']] = $this->common->get_field_from_key('sys_m_role', 'description', $val['key_sys_role']);
		}
				
		$content_data = array('unit_controller' => get_unit_controller($this),
			'current_role' => $this->common->current_role_key(),
			'roles' => $roles,
			'referrer' => $this->referrer);
		
		$data = array(
			'application' => '',
			'application_title' => '',
			'page' => 'Change User Role',
			'navigation' => $this->load->view('nav/default', '', true),
			'content' => $this->load->view('form/change_role', $content_data, true),		
		);
		
		$this->session->set_flashdata('referrer', $this->referrer);
		
		$this->load->view('layout/default', $data);		
	}
	
	function process_change_role()
	{
		$_role = $this->input->post('role');

		$role = $this->common->get_id_from_criteria('sys_x_user_role', array('key_sys_user' => $this->common->current_user_key(), 'key_sys_role' => $_role));
		$role_desc = $this->common->get_field_from_key('sys_m_role', 'description', $_role);
		if ($role) {
			$this->session->set_userdata('role', $_role);
			$this->session->set_userdata('role_desc', $role_desc);
		}
		
		$this->db->update('sys_m_user', array('id_sys_user_role' => $role), array('nkey' => $this->common->current_user_key()));
		
		$this->common->flash_message('Role changed to '.$role_desc, -1);
		
		redirect($this->referrer);
	}
	
	function send_password()
	{
	
		$content_data = array('unit_controller' => get_unit_controller($this),
			'current_role' => $this->common->current_role_key(),
			'roles' => $roles,
			'referrer' => $this->referrer);

	
		$data = array(
			'page' => 'Send User Password',
			'content' => $this->load->view('form/send_password', $content_data, true),
			
		);
		
		$this->load->view('layout/default', $data);
	}
	
	function process_send_password()
	{
		$user = $this->input->post('forgotten_user');
		$email = $this->common->get_field_from_key('sys_m_user', 'email', $user);
		
		if (!$email) {
			$this->common->flash_message('Username and/or email address not found. Please contact administrator for further information.', 1);
		} else {
			// SEND EMAIL			
			$this->email->from('password@si.del.ac.id');
			$this->email->to($email);
			
			$this->email->subject('SI Del Password');
			$this->email->message('Password: '.$this->common->get_field_from_key('sys_m_user', 'password', $user));

			$email_status = $this->email->send();
			
			if (!$email_status) {
				// failed
				$this->common->flash_message('Sending password failed, please try again later or contact administrator if problem persists.', 1);
			} else {
				// success!
				$this->common->flash_message('Password sent to <strong>'.$email.'</strong>', -1);
			}
		}
		
		redirect($this->referrer);		
	}
	
	function view_profile()
	{
		$data = array(
			'application' => '',
			'application_title' => '',
			'page' => 'Profile',
			'navigation' => $this->load->view('nav/default', '', true),
			'content' => $this->load->view('viewer/view_profile', '', true),
			
		);
		
		$this->load->view('layout/default', $data);		
	}	
}
